<template>
	<div class="content-wrap">
		<div class="content-top-box">
			<!-- 搜索区域 -->
			<ul class="search-container">
				<li>
					<span class="search-item-name">手机号：</span>
					<el-input class="search-item-content search-item-text" placeholder="请输入手机号" v-model="phone" clearable maxlength="11" @clear="clearPhoneFn">
					</el-input>
				</li>
				<li>
					<span class="search-item-name">姓名：</span>
					<el-input class="search-item-content search-item-text" placeholder="请输入姓名" v-model="userName" clearable @clear="clearUserName">
					</el-input>
				</li>
				<li>
					<span class="search-item-name">求购时间：</span>
					<el-date-picker v-model="date" type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" value-format="yyyy-MM-dd HH:mm:ss" @change="changeDate">
					</el-date-picker>
				</li>
				<li>
					<span class="search-item-name">求购状态：</span>
					<el-select class="search-item-content" v-model="isStatu" placeholder="请选择" clearable @change="changeBuyType">
						<el-option v-for="item in buyTypeOptions" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</li>
				<li>
					<el-button class="search-btn" type="primary" icon="el-icon-search" @click="searchFn" v-if="searchBtn">查询</el-button>
					<download-excel class="export-excel-wrapper" :data="jsonData" :fields="jsonFields" name="求购订单表.xls" v-if="exportBtn">
						<!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
						<el-button class="search-btn" type="primary" icon="el-icon-download">导出</el-button>
					</download-excel>
					<!-- <el-button class="search-btn" type="primary" icon="el-icon-download" @click="exportFn">导出</el-button> -->
				</li>
			</ul>
		</div>

		<div class="content-bottom-box">
			<!-- 表格 -->
			<div class="content-form-box">
				
				<el-table v-loading="loadingFlag" :data="tableData" :height="tableHeight" ref="table" border stripe style="width: 100%">
					<el-table-column prop="serialNumber" label="序号" width="60" :show-overflow-tooltip="true" fixed>
					</el-table-column>
					<el-table-column prop="createTime" label="求购时间" width="160" :show-overflow-tooltip="true" fixed>
					</el-table-column>
					<el-table-column prop="status" label="求购状态" width="120" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="wantUserName" label="姓名" :show-overflow-tooltip="true" width="80">
					</el-table-column>
					<el-table-column prop="salesPrice" label="求购单价(元/kg)" width="130">
					</el-table-column>
					<el-table-column prop="productWeight" label="整牛重量(kg/头)" width="130" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="totalPrice" label="整牛单价(元/头)" width="130" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="deliveryTimeM" label="交货期"  width="120">
					</el-table-column>
					<el-table-column prop="cattleAddress" label="养殖地区" :show-overflow-tooltip="true" width="140">
					</el-table-column>
					<el-table-column prop="totalNum" label="求购数量(头)" width="120" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="payMoney" label="求购金额(元)" width="120" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="expressFee" label="物流费用(元)" width="120" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="payMoney" label="支付金额(元)" width="120" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="payMethod" label="支付方式" width="100" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="wantUserPhone" label="手机号" width="120" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="addressArea" label="收件地址" width="200" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="outTradeNo" label="订单编号" width="180" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column prop="contractCode" label="合同编号" width="180" :show-overflow-tooltip="true">
					</el-table-column>
					<el-table-column label="操作" fixed="right">
						<template slot-scope="scope">
							<el-button @click="checkUserInfoFn(scope.row)" type="text" size="small" v-if="detailBtn">详情</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>

			<!-- 页码 -->
			<div class="content-Page-container">
				<BaseFormPage :pagination="pagination" @cFirstPageFn="cFirstPageFn" @cLastPageFn="cLastPageFn" @cJumpPageFn="cJumpPageFn" @cPreFn="cPreFn" @cNextFn="cNextFn" @cRefreshFn="cRefreshFn">
				</BaseFormPage>
			</div>
		</div>

		<!-- 弹窗 -->
		<el-dialog title="求购详情" :visible.sync="dialogFlag" :show-close="false" width="700px">
			<div class="dialog-box">
				<p class="list-title">求购信息</p>
				<div class="info-detail-box">
					<ul class="info-detail-list">
						<li>
							<span class="info-tag-name">姓名：</span>
							<span class="info-tag-text">{{detailData.wantUserName}}</span>
						</li>
						<li>
							<span class="info-tag-name">手机号：</span>
							<span class="info-tag-text">{{detailData.wantUserPhone}}</span>
						</li>
						<li>
							<span class="info-tag-name">性别：</span>
							<span class="info-tag-text">{{detailData.wantUserGender}}</span>
						</li>
						<li>
							<span class="info-tag-name">订单编号：</span>
							<span class="info-tag-text">{{detailData.outTradeNo}}</span>
						</li>
						<li>
							<span class="info-tag-name">支付时间：</span>
							<span class="info-tag-text">{{detailData.completeTime}}</span>
						</li>
						<li>
							<span class="info-tag-name">求购状态：</span>
							<span class="info-tag-text">{{detailData.status}}</span>
						</li>
						<li>
							<span class="info-tag-name">养殖周期：</span>
							<span class="info-tag-text">{{detailData.timeLimit}}{{detailData.timeType}}</span>
						</li>
						<li>
							<span class="info-tag-name">交货期：</span>
							<span class="info-tag-text" v-if="detailData.deliveryTimeM !=null">{{detailData.deliveryTimeM}}个月</span>
						</li>
						<li>
							<span class="info-tag-name">求购数量(头)：</span>
							<span class="info-tag-text">{{detailData.totalNum}}</span>
						</li>
						<li>
							<span class="info-tag-name">耳标号：</span>
							<span class="info-tag-text">{{detailData.cattleIdCard}}</span>
						</li>
						<li>
							<span class="info-tag-name">整牛单价(元/头)：</span>
							<span class="info-tag-text">{{detailData.totalPrice}}</span>
						</li>
						<li>
							<span class="info-tag-name">整牛重量(kg/头)：</span>
							<span class="info-tag-text">{{detailData.productWeight}}</span>
						</li>
					</ul>

					<ul class="info-detail-list">
						<li>
							<span class="info-tag-name">求购单价(元/kg)：</span>
							<span class="info-tag-text">{{detailData.salesPrice}}</span>
						</li>
						<li>
							<span class="info-tag-name">养殖地区：</span>
							<span class="info-tag-text">{{detailData.cattleAddress}}</span>
						</li>
						<li>
							<span class="info-tag-name">养殖牧场：</span>
							<span class="info-tag-text">{{detailData.farmName}}</span>
						</li>
						<li>
							<span class="info-tag-name">求购金额(元)：</span>
							<span class="info-tag-text" v-if="detailData.totalNum !='' && detailData.totalPrice !=''">
								{{detailData.totalNum * detailData.totalPrice}}
							</span>
						</li>
						<li>
							<span class="info-tag-name">物流费用(元)：</span>
							<span class="info-tag-text">{{detailData.expressFee}}</span>
						</li>
						<li>
							<span class="info-tag-name">支付金额(元)：</span>
							<span class="info-tag-text">{{detailData.payMoney}}</span>
						</li>
						<li>
							<span class="info-tag-name">支付方式：</span>
							<span class="info-tag-text">{{detailData.payMethod}}</span>
						</li>
						<li>
							<span class="info-tag-name">牛胴体重量(kg/头)：</span>
							<span class="info-tag-text">{{detailData.productCarcass}}</span>
						</li>
						<li>
							<span class="info-tag-name">交货牛胴体(kg)：</span>
							<span class="info-tag-text" v-if="detailData.productCarcass !=='' && detailData.totalNum != ''">{{detailData.productCarcass * detailData.totalNum}}</span>
						</li>
						<li>
							<span class="info-tag-name">物流状态：</span>
							<span class="info-tag-text">{{detailData.logisticsStatus}}</span>
						</li>
						<li>
							<span class="info-tag-name">合同编号：</span>
							<span class="info-tag-text">{{detailData.contractCode}}</span>
						</li>
						<li class="last-oli">
							<span class="info-tag-name last-ospan">收件地址：</span>
							<span class="info-tag-text last-span2">{{detailData.addressArea}}</span>
						</li>
					</ul>
				</div>
				<div class="dialog-btn">
					<el-button type="primary" size="medium" @click="dialogFlag = false">关闭</el-button>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import Storage from "@/utils/storage";
	import BaseFormPage from '@/components/BaseFormPage';
	export default {
		components: {
			BaseFormPage
		},
		inject: ['reload'],
		data() {
			return {
				loadingFlag: false, //表格数据加载状态
				dialogFlag: false, //弹窗显示状态
				phone: '',
				userName: '',
				isStatu: '',    //  求购状态 1 求购中 2代付款 3已完成
				startCreateTime: '',
				endCreateTime: '',
				resaleCategory: '', // 审核状态 1=待审核 2=审核通过 3=审核不通过
				buyType: '',
				date: '',
				buyTypeOptions: [{
						value: '1',
						label: '求购中'
					},
					{
						value: '2',
						label: '待付款'
					},
					{
						value: '5',
						label: '已完成'
					},
					{
						value: '99',
						label: '已取消'
					}
				],
				pageIndex: 1,
				pageSize: 20,
				tableHeight: 200, //表格初始高度
				tableData: [], //表格数据
				pagination: { //分页数据
					loadingFlag: false, //数据加载状态
					currentPage: 1, //当前页
					totalPage: 1, //总页数
					totalNum: 0, //总条数
					firstNum: 0, //分页显示起始值
					endNum: 0 //分页显示结束值
				},
				jsonData: [],
				jsonFields: {
					"序号": "index",
					"求购时间": "createTime",
					"求购状态": "status",
					"姓名": "wantUserName",
					"求购单价(元/kg)": "salesPrice",
					"整牛重量(kg/头": "productWeight",
					"整牛单价(元/头)": "totalPrice",
					"交货期": "deliveryTimeM",
					"养殖地区": "cattleAddress",
					"求购数量(头)": "totalNum",
					"求购金额(元)": "payMoney",
					"物流费用(元)": "expressFee",
					"支付金额(元)": "payMoney",
					"支付方式": "payMethod",
					"手机号": "wantUserPhone",
					"收件地址": "addressArea",
					"订单编号": "outTradeNo",
					"合同编号": "contractCode",
				},
				jsonMeta: [
					[{
						"key": "charset",
						"value": "utf-8"
					}]
				],
				detailId: '',
				detailData: {
					
				},

				searchBtn: (JSON.parse(Storage.get('roleAuthIdArr'))).indexOf(219) == -1 ? false : true,
				exportBtn: (JSON.parse(Storage.get('roleAuthIdArr'))).indexOf(220) == -1 ? false : true,
				detailBtn: (JSON.parse(Storage.get('roleAuthIdArr'))).indexOf(221) == -1 ? false : true,
			}
		},
		created() {
			this.getListData()
		},
		methods: {
			getListData() { // 列表
				this.loadingFlag = true;
				this.pagination.loadingFlag = true;
				let params = {
					phone: this.phone,
					userName: this.userName,
					isStatu: this.isStatu,
					startCreateTime: this.startCreateTime,
					endCreateTime: this.endCreateTime,
					pgCt: this.pageIndex,
					pgSz: this.pageSize
				}
				if(!this.phone) {
					delete params.phone
				}
				if(!this.userName) {
					delete params.userName
				}
				if(!this.isStatu) {
					delete params.isStatu
				}
				if(!this.resaleCategory) {
					delete params.resaleCategory
				}
				if(!this.startCreateTime) {
					delete params.startCreateTime
				}
				if(!this.endCreateTime) {
					delete params.endCreateTime
				}
				this.$http.get('/order/wank/list', params)
					.then((res) => {
						console.log(res.data.records)
						if(res.data.records.length > 0) {
							this.pagination.totalPage = res.data.pages;
							this.pagination.totalNum = res.data.total;
							res.data.records.forEach((item, index) => {
								//列表下标
								item.index = index;
								item.deliveryMonth = item.deliveryMonth + '个月';
								if(item.status == 1){ // 求购状态
									item.status = '求购中'
								} else if(item.status == 2){
									item.status = '待付款'
								} else if(item.status == 3){
									item.status = '养殖中'
								} else if(item.status == 4){
									item.status = '待发货'
								} else if(item.status == 5){
									item.status = '已完成'
								}else if(item.status == 99){
									item.status = '已取消'
								}
								item.deliveryTimeM = item.deliveryTimeM + '个月'
								if(item.createTime){
									item.createTime = item.createTime.replace('T', ' ')
								}
								if(item.timeType == 1){  //交割周期单位1:月2：周3：天 ,
									item.timeType = '个月'
								} else if(item.timeType == 2){
									item.timeType = '周'
								}else if(item.timeType == 3){
									item.timeType = '天'
								}
								if(item.payMethod == 'overpay'){
									item.payMethod = '余额支付'
								} else if(item.payMethod == 'wechat'){
									item.payMethod = '微信支付'
								}
								//列表序号
								item.serialNumber = ((this.pageIndex - 1) * this.pageSize) + (index + 1)
								//无数据处理
//								item.orderRefNo = item.orderRefNo || '--'
							})
							this.pagination.firstNum = res.data.records[0].serialNumber
							this.pagination.endNum = res.data.records[res.data.records.length - 1].serialNumber
						} else {
							this.pagination.currentPage = 1
							this.pagination.totalPage = 1
							this.pagination.totalNum = 0
							this.pagination.firstNum = 0
							this.pagination.endNum = 0
						}
						this.tableData = res.data.records;
					})
					.then(() => {
						if(this.tableData.length !== 0){
							this.tableHeightFn();
						}
						this.loadingFlag = false;
						this.pagination.loadingFlag = false;
						this.getAllListData()
					})

			},
			getDetailData(){ // 详情
				this.$http.get('/order/wank/get/' + this.detailId)
				.then(({data})=>{
					if(data.status == 1){ // 求购状态
						data.status = '求购中'
					} else if(data.status == 2){
						data.status = '待付款'
					} else if(data.status == 3){
						data.status = '养殖中'
					} else if(data.status == 4){
						data.status = '待发货'
					} else if(data.status == 5){
						data.status = '已完成'
					}else if(data.status == 99){
						data.status = '已取消'
					}
					if(data.timeType == 1){  //交割周期单位1:月2：周3：天 ,
						data.timeType = '个月'
					} else if(data.timeType == 2){
						data.timeType = '周'
					}else if(data.timeType == 3){
						data.timeType = '天'
					}
					if(data.payMethod == 'overpay'){
						data.payMethod = '余额支付'
					} else if(data.payMethod == 'wechat'){
						data.payMethod = '微信支付'
					}
					this.detailData = data;
					
					console.log(data)
				})
			},
			tableHeightFn() { //表格高度
				let tableDataLength = this.tableData.length;
				let minHeight = tableDataLength == 0 ? 0 : 80;
				this.$nextTick(function() {
					this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - minHeight;
					if(this.tableData.length <= 0) {
						this.tableHeight = '60vh'
					}

					let self = this;
					window.onresize = function() {
						self.tableHeight = window.innerHeight - self.$refs.table.$el.offsetTop - minHeight;
						self.tableHeight = self.tableHeight < 200 ? 200 : self.tableHeight;
					}
				})
			},
			resetPageIndex() { //重置页码
				this.pagination.currentPage = 1;
				this.pageIndex = 1;
			},
			clearPhoneFn() { //清空手机号
				this.resetPageIndex()
				this.getListData()
			},
			clearUserName() { //清空姓名
				this.resetPageIndex()
				this.getListData()
			},
			changeDate() { //选择时间
				if(this.date) {
					this.startCreateTime = this.date[0]
					this.endCreateTime = this.date[1].replace('00:00:00', '23:59:59')
				} else {
					this.startCreateTime = ''
					this.endCreateTime = ''
				}
				this.resetPageIndex()
				this.getListData()
			},
			changeBuyType() { //选择状态
				this.resetPageIndex()
				this.getListData()
			},
			searchFn() { //搜索
				this.resetPageIndex()
				this.getListData()
			},
			checkUserInfoFn(item) { //查看详情
				console.log(item)
				this.detailId = item.id;
				this.getDetailData() // 调详情接口
				this.dialogFlag = true;
			},
			cFirstPageFn(pageNum) { //首页
				this.pageIndex = pageNum;
				this.getListData()
			},
			cLastPageFn(pageNum) { //尾页
				this.pageIndex = pageNum;
				this.getListData()
			},
			cJumpPageFn(pageNum) { //页码跳转
				this.pageIndex = pageNum;
				this.getListData()
			},
			cPreFn(pageNum) { //上一页
				this.pageIndex = pageNum;
				this.getListData()
			},
			cNextFn(pageNum) { //下一页
				this.pageIndex = pageNum;
				this.getListData()
			},
			resetParams() { //重置参数
				
			},
			cRefreshFn() { //刷新
				this.reload()
			},
			getAllListData() { // 列表
				let params = {
					phone: this.phone,
					userName: this.userName,
					isStatu: this.isStatu,
					startCreateTime: this.startCreateTime,
					endCreateTime: this.endCreateTime,
					pgCt: 1,
					pgSz: 99999
				}
				if(!this.phone) {
					delete params.phone
				}
				if(!this.userName) {
					delete params.userName
				}
				if(!this.isStatu) {
					delete params.isStatu
				}
				if(!this.resaleCategory) {
					delete params.resaleCategory
				}
				if(!this.startCreateTime) {
					delete params.startCreateTime
				}
				if(!this.endCreateTime) {
					delete params.endCreateTime
				}
				this.$http.get('/order/wank/list', params)
					.then((res) => {
						console.log(res.data.records)
						if(res.data.records.length > 0) {
							res.data.records.forEach((item, index) => {
								//列表下标
								item.index = index + 1
								item.deliveryMonth = item.deliveryMonth + '个月';
								if(item.status == 1){ // 求购状态
									item.status = '求购中'
								} else if(item.status == 2){
									item.status = '待付款'
								} else if(item.status == 3){
									item.status = '养殖中'
								} else if(item.status == 4){
									item.status = '待发货'
								} else if(item.status == 5){
									item.status = '已完成'
								}else if(item.status == 99){
									item.status = '已取消'
								}
								item.deliveryTimeM = item.deliveryTimeM + '个月'
								if(item.createTime){
									item.createTime = item.createTime.replace('T', ' ')
								}
								if(item.timeType == 1){  //交割周期单位1:月2：周3：天 ,
									item.timeType = '个月'
								} else if(item.timeType == 2){
									item.timeType = '周'
								}else if(item.timeType == 3){
									item.timeType = '天'
								}
								if(item.payMethod == 'overpay'){
									item.payMethod = '余额支付'
								} else if(item.payMethod == 'wechat'){
									item.payMethod = '微信支付'
								}
								//列表序号

								//无数据处理
//								item.orderRefNo = item.orderRefNo || '--'
							})
						}
						this.jsonData = res.data.records;
					})

			},
		}
	}
</script>

<style lang="scss" scoped>
	.dialog-box {
		.list-title {
			width: 100%;
			margin-bottom: 20px;
			padding-left: 20px;
			box-sizing: border-box;
			color: #344047;
			font-weight: 600;
		}
		.info-detail-box {
			width: 100%;
			display: flex;
			.info-detail-list {
				width: 50%;
				&:nth-of-type(1) {
					padding-right: 5px;
					box-sizing: border-box;
				}
				&:nth-of-type(2) {
					padding-left: 5px;
					box-sizing: border-box;
				}
				li {
					width: 100%;
					display: flex;
					align-items: center;
					margin-bottom: 12px;
					.info-tag-name {
						width: 144px;
						text-align: right;
					}
					.info-tag-text {}
				}
			}
		}
		.dialog-btn {
			width: 100%;
			padding-top: 28px;
			text-align: center;
		}
	}
	.last-oli{
		display: inherit; 
		padding-left: 74px;
		position: relative;
		.last-ospan{
			position: absolute;
			left: 0;
			top: 0;
		}
		.last-span2{
			position: absolute;
			left: 145px;
			top: 0;
		}
	}
</style>